/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CastleDefense;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import java.util.Vector;


/**
 *
 * @author Firedemon
 */
public class HelpBox
{
	int      boardW, boardH, w = 600, h = 400;
	String   helpTxt; 
	Image    img; 
	Graphics g;  

	public HelpBox(String helpTxt, Image img, int W, int H, Graphics g)
	{
		this.helpTxt = helpTxt;
		this.boardW  = 882;
		this.boardH  = 966;
		this.img     = img;
		this.g       = g;
	}

	public BufferedImage getHelpBox()
	{
		Graphics2D g2d = (Graphics2D)g;
		g2d.setFont(new Font("Verdana", Font.PLAIN, 18));
		FontMetrics fm = g2d.getFontMetrics();

		String title = helpTxt.substring(0, helpTxt.indexOf(";"));

		String[] TEXT  = wrapText(helpTxt.substring(helpTxt.indexOf(";")+1), 55);
		int textWidth, 
		centerBW, 
		centerBH, 
		textHeight = 20;

		int centerW    = (boardW-getW())/2;
		int centerH    = (boardH-getH())/2;

		//Draw help box
		g2d.setColor(Color.white);
		g2d.setStroke(new BasicStroke(2.0f));
		g2d.drawRect(centerW, centerH, w, h);
		g2d.setColor(new Color(0,0,0,150));
		g2d.fillRect(centerW, centerH, w+1, h+1);

		//Draw help text title
		textWidth = fm.stringWidth(title);
		centerBW  = (centerW+(getW()/2))-(textWidth/2);
		centerBH  = 20+centerH+(textHeight*(1));
		g2d.setColor(Color.white);
		g2d.drawString(title, centerBW, centerBH);

		//Draw help text
		for(int i=0; i<TEXT.length; i++)
		{
			textWidth = fm.stringWidth(TEXT[i]);

			centerBW  = (centerW+(getW()/2))-(textWidth/2);
			centerBH  = 50+centerH+(textHeight*(i+1));  

			g2d.setColor(Color.white);
			g2d.drawString(TEXT[i], centerBW, centerBH);
		}

		BufferedImage helpBox = g2d.getDeviceConfiguration().createCompatibleImage(
				w, h, Transparency.BITMASK);

		return helpBox;
	}

	static String [] wrapText (String text, int len)
	{
		//remove double spaces
		text = text.replaceAll("  ", "");
		// return empty array for null text
		if (text == null)
			return new String [] {};

		// return text if len is zero or less
		if (len <= 0)
			return new String [] {text};

		// return text if less than length
		if (text.length() <= len)
			return new String [] {text};

		char[] chars = text.toCharArray();
		Vector<String> lines = new Vector<String>();
		StringBuilder  line  = new StringBuilder();
		StringBuffer   word  = new StringBuffer();

		for (int i = 0; i < chars.length; i++) 
		{
			if (chars[i] != '\\') 
				word.append(chars[i]);

			if (chars[i] == '\\')
			{
				line.append(word);
				word.delete(0, word.length());
				
				lines.add(line.toString());
				line.delete(0, line.length());
			}
			
			if (chars[i] == ' ') 
			{
				if ((line.length() + word.length()) > len) 
				{
					lines.add(line.toString());
					line.delete(0, line.length());
				}

				line.append(word);
				word.delete(0, word.length());
			}	
		}

		// handle any extra chars in current word
		if (word.length() > 0) 
		{
			if ((line.length() + word.length()) > len)
			{
				lines.add(line.toString());
				line.delete(0, line.length());
			}

			line.append(word);
		}

		// handle extra line
		if (line.length() > 0) 
		{
			lines.add(line.toString());
		}

		String[] ret = new String[lines.size()];
		
		int c = 0; // counter
		for (Enumeration<String> e = lines.elements(); e.hasMoreElements(); c++) 
		{
			ret[c] = (String) e.nextElement();
		}

		return ret;
	}

	public int getW()
	{
		return w;
	}

	public int getH()
	{
		return h;
	}
}
